<div class="clear_both"></div>
<div class="main_container">
<?php 
	$regular_hours = 8;
	$total_reg_hours = $approved_ot = $total_late = $total_approved_ot_hours = $total_approved_ut_hours = 0;

?>
<p class="text_uppercase native_f14 font_bold">Name:
<?php 
	if(isset($timesheet)) echo $timesheet[0]->first_name ." ".$timesheet[0]->middle_name ." ". $timesheet[0]->last_name;
	
?><br>
Period: <?php echo DateUtils::formatDate($period->date_start, ConstantsUtils::$DATE_FRIENDLY_FORMAT). " - " .DateUtils::formatDate($period->date_end, ConstantsUtils::$DATE_FRIENDLY_FORMAT) ;?>
</p>
<?php if(isset($timesheet)):?>

<table border="1" cellspacing="0" cellpadding="5" id="members" class="display">
<thead>
	<th>Date</th>
	<th>Time In</th>
	<th>Time Out</th>
	<th># Hours</th>
	<th>Regular Hours</th>
	<!-- <th># Overtime</th>  -->
	<th># Approved OT</th>
	<th># Approved UT</th>
	<!-- <th># Earlier</th>  -->
	<th># Late</th>
	<th>Remarks</th>
	<th>Status</th>
	<th>Action</th>
	
</thead>
<?php 
$confirm = true;
foreach($timesheet as $ts) {
	if(isset($rest_day[0])) {
		$rest = DateUtils::theSameDay($ts->date, $rest_day[0]->day);
			
	}
	$class = (isset($rest) && $rest)?'rest_day':'';
	$reg_hours = (DateUtils::time_to_decimal($ts->total_hours) > 0)?$regular_hours:"--";
	$approved_ot_hours = (isset($ts->approved_overtime) && DateUtils::time_to_decimal($ts->approved_overtime) > 0)?DateUtils::time_to_decimal($ts->approved_overtime):"--";
	$approved_ut_hours = (isset($ts->approved_undertime) && DateUtils::time_to_decimal($ts->approved_undertime) > 0)?DateUtils::time_to_decimal($ts->approved_undertime):"--";
	$total_approved_ot_hours += intval($approved_ot_hours);
	$total_approved_ut_hours += intval($approved_ut_hours);
	$total_late += (isset($ts->late) && DateUtils::time_to_decimal($ts->late) > 0)?$ts->late:"--";
	$total_reg_hours += intval($reg_hours);
	$reviewed = $ts->is_saved ? "<img src='/images/common/check.png' />":'--';
	$confirm &= $ts->is_saved;
	?>
<tr class="timesheet_details">
	<td><span class="<?php echo $class?>"><?php echo $ts->date?></span></td>
	<td><?php echo ($ts->time_in == ConstantsUtils::$EMPTY_DATE_TIME)?'--': substr($ts->time_in, 11);?></td>
	<td><?php echo ($ts->time_out == ConstantsUtils::$EMPTY_DATE_TIME)?'--': substr($ts->time_out, 11);?></td>
	<td><?php echo $ts->total_hours?></td>
	<td><?php echo $reg_hours;?></td>
	<!-- <td><?php echo $ts->overtime?></td> -->
	
	<td><?php echo ($ts->approved_overtime == ConstantsUtils::$EMPTY_TIME)?'--': $ts->approved_overtime;?></td>
	 <td><?php echo ($ts->approved_undertime == ConstantsUtils::$EMPTY_TIME)?'--': $ts->approved_undertime;?></td>
	<!-- <td><?php echo ($ts->earlier == ConstantsUtils::$EMPTY_TIME)?'--': $ts->earlier;?></td> -->
	<td><?php echo ($ts->late == ConstantsUtils::$EMPTY_TIME)?'--': $ts->late;?></td>
	<td class="<?php echo StringUtils::addUnderscore(strtolower($ts->remarks_name));?>">
	<?php echo $ts->remarks_name?>
	</td>
	<td><?php echo $reviewed?></td>
	<td><a href="javascript:;" onClick="showDetails(<?php echo $ts->id;?>)" title="Edit"><img src="/images/common/edit_icon.png" border="0"></a></td>
</tr>

<?php }?>
<tr style="background-color: #666; color:#ddd; font-weight:bold">
	<td>TOTAL</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><b><?php echo $total_reg_hours;?></b></td>
	<td><b><?php echo CommonUtils::number_format($total_approved_ot_hours / 60);?></b></td>
	<td><b><?php echo CommonUtils::number_format($total_approved_ut_hours / 60);?></b></td>
	<td><b><?php echo $total_late;?></b></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
</tr>
</table>
<br />
<div class="right_button">
<?php if($confirm):?>
	<input type="button" value="Confirm" id="confirm_button"> 
<?php endif;?>
	
<?php if($timesheet_payroll):?>
	<input type="button" value="Export" id="timesheet_export">
<?php endif;?>
<input type="button" value="Print"> 
</div>
<fieldset class="legend">
	<legend>Legend</legend>
	<span class='rest_day'>Date</span> - Rest day
</fieldset>
<?php endif;?>
<?php echo form_open('report/export', array('id'=>'employee_timesheet'));?>
<?php echo form_hidden("template", "employee_timesheet");?>
<?php echo form_hidden("user_id", $user_id);?>
<?php echo form_hidden("payroll_period_id", $period->id);?>
<?php echo form_close();?>
<!-- user input dialog -->
<button class="modalInput" rel="#yesno" id="button_confirm" style="display:none">Yes or no?</button>

<div class="modal_box" id="yesno" style="display:none;">
  <div>
    <h2>Confirmation</h2>
    <p class="loading_message">
      This dialog is opened programmatically when the page
      loads. There is no need for a trigger element.
    </p>
    <p>
    	<button class="close"> Yes </button>
    	<button class="close"> No </button>
  	</p>
  </div>
</div>

</div>

<script language="javascript">
$(document).ready(function() {
	$('#members').dataTable({
		/*"sPaginationType": "full_numbers",*/
		"iDisplayLength":25,
		"bSort": false,
		"bPaginate": false
	});

	$("#members_filter label").hide();
	$("#members_info").hide();

	$("#confirm_button").click(function(){
		//var answer = confirm("Data are CORRECT and will be submitted for payroll.");
		$('div.modal_box p.loading_message').html('Data are CORRECT and will be submitted for payroll?');
		$('#button_confirm').trigger('click');
	})


	var triggers = $(".modalInput").overlay({
	      mask: {
	        color: '#ebecff',
	        loadSpeed: 200,
	        opacity: 0.9
	      },
	      closeOnClick: false
	  });
	  
	    var buttons = $("#yesno button").click(function(e) {
	      var yes = buttons.index(this) === 0;
	      if(yes){
	    	  $.get('<?php echo base_url()?>payroll/view_user/<?php echo $period->id?>/<?php echo $user_id?>', function(data) {
	 			// $('p.loading_message').html('Data successfully saved!');
	 				overlay_init();
		 			setMessageOverlay("Data successfully saved!");
		 			loadOverlay();
		 			setTimeout("unloadOverlay()",1000);
	 			});
	      }
	      //triggers.eq(0).html("You clicked " + (yes ? "yes" : "no"));
	  });



	$('#timesheet_export').click(function(){
		$('#employee_timesheet').submit();
	})
} );


function showDetails(id){
	var url = '<?php echo base_url()?>timesheet/details/<?php echo $user_id?>/' + id;
	$.fancybox.open({
		href : url,
		type : 'iframe',
		padding : 10,
		height: '92%',
		autoSize	: false,
		helpers:  {
			overlay : {
				opacity  : 0.4,
				closeClick: false
			}
	    },
	    'afterClose': function(){document.location = document.location}
	});
}

</script>
<?php 
	//$time2 = CommonUtils::getCurrentTime();
	//$diff = $time2 - $start_time;
	//echo "processed: " . $diff ." seconds";
?>